package BandB.Tool.GPhoneManager.TaskManager;

import java.util.ArrayList;

import BandB.Tool.GPhoneManager.R;
import BandB.Tool.GPhoneManager.TaskManager.Util.DataHandler;
import BandB.Tool.GPhoneManager.TaskManager.Util.Rotate3dAnimation;
import BandB.Tool.GPhoneManager.TaskManager.Util.SimpleAppInfo;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.Animation.AnimationListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class BlacklistActivity extends Activity {

	protected static final String TAG = "BlacklistActivity";
	private BlacklistAdapter adapter;
	private ArrayList<SimpleAppInfo> mSimpleAppList;

	private View showLoadingView;
	private ListView blacklistView;
	private View showOperateView;
	private Button btnRemove, btnBack;
	private ImageView operateAppIcon;
	private TextView operateAppName;

	private Rotate3dAnimation animationForMore;
	private Rotate3dAnimation animationBack;
	private DataHandler mDataHandler;
	private static final int centerX = 160;
	private static final int centerY = 0;

	private static int currentMode = -1;
	private static ApplicationInfo currentApplicationInfo;
	private static String currentAppPackageName;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.blacklist);
		Log.e(TAG, "onCreate called");
		initViews();
		setViewDisplay(MODE_SHOW_LOADING);

		mDataHandler = new DataHandler(this);
		animationForMore = new Rotate3dAnimation(0, 90, centerX, centerY, 0,
				true);
		animationForMore.setInterpolator(new LinearInterpolator());
		animationForMore.setDuration(500);
		animationForMore.setAnimationListener(animationForMoreListener);

		animationBack = new Rotate3dAnimation(-90, 0, centerX, centerY, 0, true);
		animationBack.setInterpolator(new LinearInterpolator());
		animationBack.setDuration(500);

		blacklistView.setOnItemClickListener(listViewItemClickedListener);

		mHandler.sendEmptyMessage(MSG_LOAD_BLACKLIST_BEGIN);
	}

	private void initViews() {
		showLoadingView = findViewById(R.id.showLoadingView);
		blacklistView = (ListView) findViewById(R.id.blacklistView);
		showOperateView = findViewById(R.id.operateView);
		btnRemove = (Button) findViewById(R.id.btnRemove);
		btnRemove.setOnClickListener(btnClickListener);
		btnBack = (Button) findViewById(R.id.btnBack);
		btnBack.setOnClickListener(btnClickListener);
		operateAppIcon = (ImageView) findViewById(R.id.operateAppIcon);
		operateAppName = (TextView) findViewById(R.id.operateAppName);
	}

	OnClickListener btnClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch (v.getId()) {
			case R.id.btnRemove:
				mDataHandler.removeFromBlacklist(currentAppPackageName);
				mHandler.sendEmptyMessage(MSG_LOAD_BLACKLIST_BEGIN);
				break;
			case R.id.btnBack:
				setViewDisplay(MODE_SHOW_LIST);
				break;
			default:
				break;
			}
		}

	};

	private AnimationListener animationForMoreListener = new AnimationListener() {

		@Override
		public void onAnimationEnd(Animation animation) {
			// TODO Auto-generated method stub
			setViewDisplay(MODE_SHOW_OPERATE);
			showOperateView.startAnimation(animationBack);
		}

		@Override
		public void onAnimationRepeat(Animation animation) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onAnimationStart(Animation animation) {
			// TODO Auto-generated method stub

		}

	};

	private OnItemClickListener listViewItemClickedListener = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			// TODO Auto-generated method stub
			Log.e(TAG, "item clicked");
			final int position = arg2;

			// blacklistView.setAnimation(animationForMore);
			PackageManager pManager = getPackageManager();
			try {
				currentApplicationInfo = pManager.getApplicationInfo(adapter
						.getItem(arg2).packageName, 0);

			} catch (NameNotFoundException e) {
				// TODO Auto-generated catch block
				AlertDialog.Builder builder = new AlertDialog.Builder(
						BlacklistActivity.this);
				builder.setTitle("Application not found.");
				builder
						.setMessage("This Application is probably uninstalled from your device. "
								+ "It will be removed from the blacklist.");
				builder.setPositiveButton("OK",
						new android.content.DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								// TODO Auto-generated method stub
								DataHandler dataHandler = new DataHandler(
										BlacklistActivity.this);
								try {
									dataHandler.removeFromBlacklist(adapter
											.getItem(position).packageName);
									mHandler
											.sendEmptyMessage(MSG_LOAD_BLACKLIST_BEGIN);
									Toast.makeText(BlacklistActivity.this,
											"Removed from blacklist",
											Toast.LENGTH_SHORT).show();
								} catch (Exception e2) {
									// TODO: handle exception
								}

							}

						});
				builder.show();
				e.printStackTrace();
				return;
			}
			currentAppPackageName = currentApplicationInfo.packageName;
			operateAppName.setText(currentApplicationInfo.loadLabel(pManager));
			operateAppIcon.setImageDrawable(currentApplicationInfo
					.loadIcon(pManager));
			blacklistView.startAnimation(animationForMore);

		}

	};

	private static final int MODE_SHOW_LOADING = 0;
	private static final int MODE_SHOW_LIST = 1;
	private static final int MODE_SHOW_OPERATE = 2;

	private static final int MSG_LOAD_BLACKLIST_BEGIN = 0;
	private static final int MSG_LOAD_BLACKLIST_END = 1;
	private static final int MSG_LIST_IS_EMPTY = 2;

	private void setViewDisplay(int mode) {
		switch (mode) {
		case MODE_SHOW_LOADING:
			currentMode = MODE_SHOW_LOADING;
			showLoadingView.setVisibility(View.VISIBLE);
			blacklistView.setVisibility(View.GONE);
			showOperateView.setVisibility(View.GONE);
			break;
		case MODE_SHOW_LIST:
			mSimpleAppList = mDataHandler.getSimpleInfoOfBlacklist();
			if (0 == mSimpleAppList.size()) {
				mHandler.sendEmptyMessage(MSG_LIST_IS_EMPTY);
			}
			currentMode = MODE_SHOW_LIST;
			showLoadingView.setVisibility(View.GONE);
			blacklistView.setVisibility(View.VISIBLE);
			showOperateView.setVisibility(View.GONE);
			break;
		case MODE_SHOW_OPERATE:
			currentMode = MODE_SHOW_OPERATE;
			showLoadingView.setVisibility(View.GONE);
			blacklistView.setVisibility(View.GONE);
			showOperateView.setVisibility(View.VISIBLE);
			break;
		default:
			break;
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		switch (keyCode) {
		case KeyEvent.KEYCODE_BACK:
			if (currentMode == MODE_SHOW_OPERATE) {
				setViewDisplay(MODE_SHOW_LIST);
				return true;
			}
		default:
			break;
		}
		return super.onKeyDown(keyCode, event);
	}

	private Handler mHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			switch (msg.what) {
			case MSG_LOAD_BLACKLIST_BEGIN:
				new Thread(new Runnable() {

					@Override
					public void run() {
						// TODO Auto-generated method stub
						mSimpleAppList = mDataHandler
								.getSimpleInfoOfBlacklist();
						if (0 == mSimpleAppList.size()) {
							mHandler.sendEmptyMessage(MSG_LIST_IS_EMPTY);
						} else {
							adapter = new BlacklistAdapter(
									BlacklistActivity.this, mSimpleAppList);
							mHandler.sendEmptyMessage(MSG_LOAD_BLACKLIST_END);
						}
					}
				}).start();
				break;
			case MSG_LOAD_BLACKLIST_END:
				blacklistView.setAdapter(adapter);
				setViewDisplay(MODE_SHOW_LIST);
				break;
			case MSG_LIST_IS_EMPTY:
				finish();
				break;
			default:
				break;
			}

			super.handleMessage(msg);
		}

	};
}
